﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using ATM_Pro.Module;
using System.Threading;
using ATM_Pro.Public;
using OpenCvSharp;

namespace ATM_Pro
{

    //public class VideoHandle : IVideoCompression
    //{
    //    Queue<CompressionVideoParams> queueFile = null;

    //    private string VideoPath { get; set; }
    //    private int PreMinute { get; set; }
    //    private int Width { get; set; }
    //    private int Height { get; set; }
    //    private string PicPath { get; set; }

    //    public VideoHandle()
    //    {

    //    }

    //    public VideoHandle(string path, int preMinute, int width, int height)
    //    {
    //        this.VideoPath = path;
    //        this.PreMinute = preMinute;
    //        this.Width = width;
    //        this.Height = height;
    //    }

    //    #region IVideoCompression Members

    //    //判断当前的分钟段内的视频是否存在
    //    public bool HasVideo(PacketType type)
    //    {
    //        this.PicPath = CombinPath(type);
    //        this.VideoPath = this.PicPath + ".avi";
    //        return File.Exists(this.VideoPath);
    //    }

    //    public bool HasVideo(string path)
    //    {
    //        return File.Exists(path);
    //    }

    //    protected string CombinPath(PacketType type)
    //    {
    //        string temp = Path.Combine(this.VideoPath.Substring(0, this.VideoPath.Length - 27), this.PreMinute.ToString("d2"));
    //        if (type.ToString().Equals(PacketType.AlarmMessage.ToString()))
    //        {
    //            temp = temp.Replace(type.ToString(), PacketType.RealtimeMessage.ToString());
    //            return temp;
    //        }
    //        else if (type.ToString().Equals(PacketType.TradeMessage.ToString()))
    //        {
    //            temp = temp.Replace(type.ToString() + "\\"+Properties.Settings.Default.FaceImg, PacketType.RealtimeMessage.ToString());
    //            return temp;
    //        }
    //        return "";
    //    }

    //    //压缩视频
    //    //public void CompressionImage()
    //    //{
    //    //    CvVideoWriter writer = Cv.CreateVideoWriter(this.VideoPath, "XVID", 10, new CvSize(this.Width, this.Height));
    //    //    string[] files = Directory.GetFiles(this.PicPath);

    //    //    if (files.Length > 0)
    //    //    {
    //    //        foreach (string file in files)
    //    //        {
    //    //            string name = file.Substring(file.Length - 23, 23);
    //    //            if (ImageHandle.IsCorrectImgFile(name))
    //    //            {
    //    //                IplImage ipl = new IplImage(file);
    //    //                writer.WriteFrame(ipl);
    //    //                Cv.ReleaseImage(ipl);
    //    //            }
    //    //        }
    //    //    }
    //    //    writer.Dispose();
    //    //}

    //    public void CompressionImage(object obj)
    //    {
    //        CvVideoWriter writer = Cv.CreateVideoWriter(this.VideoPath, "XVID", 10, new CvSize(this.Width, this.Height));
    //        string[] files = Directory.GetFiles(this.PicPath);

    //        if (files.Length > 0)
    //        {
    //            foreach (string file in files)
    //            {
    //                string name = file.Substring(file.Length - 23, 23);
    //                if (ImageHandle.IsCorrectImgFile(name))
    //                {
    //                    IplImage ipl = new IplImage(file);
    //                    writer.WriteFrame(ipl);
    //                    Cv.ReleaseImage(ipl);
    //                }
    //            }
    //        }
    //        writer.Dispose();
    //    }

    //    //打开视频
    //    public string GetVideoPath(string picPath)
    //    {
    //        //调用HasVideo的方法后，就可以直接调用
    //        return string.Empty;
    //    }

    //    #endregion




    public class VideoHandle
    {
        CompressionVideoParams cvp = null;

        private string videoPath = "";
        private string picPath = "";
        private string picVecPath = "";
        private string picHorPath = "";

        public VideoHandle() { }

        public VideoHandle(CompressionVideoParams cvpTemp)
        {
            this.cvp = cvpTemp;
        }

        #region IVideoCompression Members

        //public enum CameraNumber
        //{
        //    水平 = 0,
        //    垂直 = 1,
        //    全景 = 2
        //}

        //public enum PacketType
        //{
        //    AlarmMessage = 1,
        //    AlarmCallBack = 2,
        //    TradeMessage = 3,
        //    RealtimeMessage = 4
        //}

        /// <summary>
        /// 检测上一分钟是否存在报警信息或者交易信息
        /// </summary>
        /// <returns></returns>
        public void HasWarnInfoOrTransInfo()
        {
            //D:\ImageOutput\00\00\RealtimeMessage\20091107\09\36\20091107093601-0040.jpg
            //D:\ImageOutput\00\01\RealtimeMessage\20091107\09\36\20091107093600-0003.jpg
            bool tag = false;
            string path = this.cvp.Path;
            string fileName = Path.GetFileName(path);
            string temp = path.Substring(0, path.Length - fileName.Length - 1);
            string curMinute = temp.Substring(temp.Length - 2, 2);

            //this.picPath = @"D:\ImageOutput\00\00\RealtimeMessage\20091107\09\[36]->[35]";
            //this.picPath = @"D:\ImageOutput\00\01\RealtimeMessage\20091107\09\[36]->[35]";
            this.picPath = temp.Replace(curMinute, this.cvp.PreTime.AddMinutes(-1).Minute.ToString("d2"));
            System.Diagnostics.Debug.WriteLine("this.picPath = @" + this.picPath);
            DoIt(picPath);
        }

        protected void DoIt(string path)
        {
            string transPath = "";//交易录像
            string warnPath = "";//报警录像

            //生成ID为0路径
            if (this.cvp.AtmBean.Cam.ID == 0)
            {
                //path = @"D:\ImageOutput\00\00\RealtimeMessage\20091107\09\35";

                //zeroPath= @"D:\ImageOutput\00\00\TradeMessage\BigImg\20091107\09\35";
                transPath = path.Replace(PacketType.RealtimeMessage.ToString(), PacketType.TradeMessage.ToString() + "\\" + Properties.Settings.Default.BigImg);
                //onePath = @"D:\ImageOutput\00\01\AlarmMessage\20091107\09\35";
                warnPath = path.Replace("00\\" + PacketType.RealtimeMessage.ToString(), "01\\" + PacketType.AlarmMessage.ToString());
                this.picVecPath = path.Replace("00\\" + PacketType.RealtimeMessage.ToString(), "01\\" + PacketType.RealtimeMessage.ToString());
                this.picHorPath = path;

                System.Diagnostics.Debug.WriteLine("id=0  transPath = @" + transPath);
                System.Diagnostics.Debug.WriteLine("id=0  warnPath = @" + warnPath);
                System.Diagnostics.Debug.WriteLine("id=0  this.picVecPath = @" + this.picVecPath);
                System.Diagnostics.Debug.WriteLine("id=0  this.picHorPath = @" + this.picHorPath);
            }

            //生成ID为1路径
            if (this.cvp.AtmBean.Cam.ID == 1)
            {
                //path = @"D:\ImageOutput\00\01\RealtimeMessage\20091107\09\35";

                //zeroPath= @"D:\ImageOutput\00\00\TradeMessage\BigImg\20091107\09\35";
                transPath = path.Replace("01\\" + PacketType.RealtimeMessage.ToString(), "00\\" + PacketType.TradeMessage.ToString() + "\\" + Properties.Settings.Default.BigImg);
                //onePath = @"D:\ImageOutput\00\01\AlarmMessage\20091107\09\35";
                warnPath = path.Replace(PacketType.RealtimeMessage.ToString(), PacketType.AlarmMessage.ToString());
                this.picVecPath = path;
                this.picHorPath = path.Replace("01\\" + PacketType.RealtimeMessage.ToString(), "00\\" + PacketType.RealtimeMessage.ToString());
                System.Diagnostics.Debug.WriteLine("id=1  transPath = @" + transPath);
                System.Diagnostics.Debug.WriteLine("id=1  warnPath = @" + warnPath);
                System.Diagnostics.Debug.WriteLine("id=1  this.picVecPath = @" + this.picVecPath);
                System.Diagnostics.Debug.WriteLine("id=1  this.picHorPath = @" + this.picHorPath);
            }
            bool zeroRes = Directory.Exists(transPath);
            bool oneRes = Directory.Exists(warnPath);

            if (!zeroRes)
                DeleteInvalidFile(this.picHorPath);
            else
                CompressionImage(this.picHorPath);

            if (!oneRes)
                DeleteInvalidFile(this.picVecPath);
            else
                CompressionImage(this.picVecPath);
        }

        /// <summary>
        /// 判断是否存在视频
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool HasVideo(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// 存在交易信息或报警信息
        /// </summary>
        public void CompressionImage(string path)
        {
            string[] files = Directory.GetFiles(path);
            if (files.Length > 1)
            {
                IplImage temp = new IplImage(files[1]);

                CvVideoWriter writer = Cv.CreateVideoWriter(path + ".avi", "XVID", 10, new CvSize(temp.Width, temp.Height));

                foreach (string file in files)
                {
                    string name = file.Substring(file.Length - 23, 23);
                    if (ImageHandle.IsCorrectImgFile(name))
                    {
                        IplImage ipl = new IplImage(file);
                        writer.WriteFrame(ipl);
                        Cv.ReleaseImage(ipl);
                    }
                }

                writer.Dispose();
            }

        }

        /// <summary>
        /// 不存在交易报警信息 删除无效的图片文件
        /// </summary>
        public void DeleteInvalidFile(string path)
        {
            FileHandle.DeleteFolder(path);
        }

        #endregion
    }

    public class CompressionVideoParams
    {
        public CompressionVideoParams() { }
        public string Path { get; set; }
        public string PacketType { get; set; }
        public ATM AtmBean { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public DateTime PreTime { get; set; }
    }
}